/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxmltest;

import java.io.File;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ResourceBundle;
import javafx.concurrent.Worker;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 *
 * @author Arttu
 */
public class FXMLDocumentController implements Initializable, EventHandler<WorkerStateEvent> {

    @FXML
    private Label label;

    private FileChooser searchDirectoryChooser;
    @FXML
    private Button ChooseFileButton;
    @FXML
    private TextField FileNameTextField;
    @FXML
    private TextArea fileDigestResultTextArea;
    @FXML
    private ProgressBar digestProgressBar;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        searchDirectoryChooser = new FileChooser();
        searchDirectoryChooser.setTitle("Select file");
    }

    @FXML
    private void handleSelectFileAction(ActionEvent event) {

        File selectedFile = searchDirectoryChooser.showOpenDialog(new Stage());

        if (selectedFile != null) {

            FileNameTextField.setText(Paths.get(selectedFile.toURI()).toString());

            FileDigest fileDigest = new FileDigest(selectedFile);
            digestProgressBar.progressProperty().bind(fileDigest.progressProperty());
            fileDigest.setOnSucceeded(this);
            new Thread(fileDigest).start();

        }
    }

    @FXML
    private void handleOnDragOver(DragEvent event) {
        Dragboard db = event.getDragboard();
        if (db.hasFiles()) {
            event.acceptTransferModes(TransferMode.COPY);
        } else {
            event.consume();
        }
    }

    @FXML
    private void handleOnDragDropped(DragEvent event) {
        Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.hasFiles()) {
            success = true;
            String filePath = null;
            for (File file : db.getFiles()) {

                filePath = file.getAbsolutePath();
                System.out.println(filePath);

                FileDigest fileDigest = new FileDigest(file);
                digestProgressBar.progressProperty().bind(fileDigest.progressProperty());
                fileDigest.setOnSucceeded(this);
                new Thread(fileDigest).start();
                
            }
        }
        event.setDropCompleted(success);
        event.consume();
    }

    @Override
    public void handle(WorkerStateEvent event) {
        if(event.getSource().getState() == Worker.State.SUCCEEDED) {
            fileDigestResultTextArea.appendText((String)event.getSource().getValue() + "\n");
        } 
    }

}
